<?php
namespace app\yangxt_forum_admin\controller;
use think\Controller;
class Advdeal extends Controller
{
    public function advDeal()
    {  
        $adv=db('adv')
        ->where('atype',0)
        ->select();
        foreach ($adv as $key => &$value) {
            if ($value['acheck']==0) {
                $value['acheck']='未审核(点击通过)';
                $value['checkcolor']='red';
                if ($value['astop']==0) {
                    $value['stopcolor']='';
                }
                else{
                     $value['stopcolor']='red';
                     $value['checkcolor']='';
                }

            }
            elseif($value['acheck']==1){
                $value['acheck']='通过审核(点击取消)';
                $value['checkcolor']='red';
                 if ($value['astop']==0) {
                    $value['stopcolor']='';
                }
                else{
                     $value['stopcolor']='red';
                     $value['checkcolor']='';
                }

            }
        };
       return view('',['adv'=>$adv]);
    }

    public function advAdd()
    { 
       return view();
    }

    public function advDoAdd()
    {  
       $file = request()->file("aimg");
       // 存储到指定路径
       $path = ROOT_PATH."public/static/img/adv/";
       $info = $file ->validate(['size'=>102400,'ext'=>'jpg,png,gif']) ->move($path);
       // 
       $advtitle=input('atext');
       $advcon=input('acontent');
       $apath=input('apath');
       if($info){
            $filename = $info ->getSaveName();
             $adv= [
            'atitle'=>$advtitle,
            'acontent'=>$advcon,
            'aimg'=>$filename,
            'apath'=>$apath,
            'atime'=>time(),
            'acheck'=>0,
            'astop'=>0,
            'atype'=>0
                ];
            $res=db("adv")
                ->insert($adv);
            if ($res) {
                $this->success("广告数据添加成功，需要进行审核才能正常使用","advdeal/advdeal");
                }
            else{
                
                $this->error("广告数据添加失败","advdeal/advdeal");
            }
        }
    }
    public function advInfo()
    {  
       
       $adv =db('adv')
       ->where('aid',input('aid'))
       ->select();

        return view('',["adv"=>$adv]);

    }
    public function advInfoModi()
    {  

        $find=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $aaa=$find['aimg'];
       $file = request()->file("aimg");
       // 存储到指定路径
       if ($file) {
       $path = ROOT_PATH."public/static/img/adv/";
       $info = $file ->validate(['size'=>102400,'ext'=>'jpg,png,gif']) ->move($path);
       }
       else{
        $info=0;
       }
       
       // 
       $advtitle=input('atext');
       $advcon=input('acontent');
       $apath=input('apath');
       if($info){
            $filename = $info ->getSaveName();
            $adv = db('adv')
            ->where('aid',input('aid'))
            ->setField(['atitle'=>$advtitle,'acontent'=>$advcon,'aimg'=>$filename,'apath'=>$apath,'atime'=>time(),
                'acheck'=>0]);
            if ($adv) {
                unlink(ROOT_PATH."public\static\img\adv\\".$aaa);
                $this->success("广告数据修改成功，需要进行审核才能正常使用","advdeal/advdeal");
                }
            else{
                
                $this->error("广告数据修改失败","advdeal/advdeal");
            }

        }else{
            $adv = db('adv')
                ->where('aid',input('aid'))
                ->setField(['atitle'=>$advtitle,'acontent'=>$advcon,'apath'=>$apath,'atime'=>time(),
                    'acheck'=>0]);
                if ($adv) {
                    $this->success("广告数据修改成功，需要进行审核才能正常使用","advdeal/advdeal");
                    }
                else{
                    
                    $this->error("广告数据修改失败","advdeal/advdeal");
                }
        }
    }
    public function advDel()
    {  

        $find=db('adv')
        ->where('aid',input('aid'))
        ->find();


        $aaa=$find['aimg'];


        $advdel=db('adv')
        ->where('aid',input('aid'))
        ->delete();
         if ($advdel) {
                unlink(ROOT_PATH."public\static\img\adv\\".$aaa);
                $this ->success('删除广告成功',url("advdeal/advdeal"));
            }
            else{
                $this ->error('删除广告失败',url("advdeal/advdeal"));
            }

    }

    public function advOK()
    {  
        $advcheck=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $check=$advcheck['acheck'];
        if ($check==0) {
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('acheck',1);
            if ($modi) {
                 $this ->success('广告审核通过',url("advdeal/advdeal"));
             } 
        }
        else{
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('acheck',0);
            if ($modi) {
                 $this ->error('广告审核取消，无法正常显示',url("advdeal/advdeal"));
             } 

        }
    

    }
    public function advStop()
    {  
        $advcheck=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $check=$advcheck['astop'];
        if ($check==0) {
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('astop',1);
            if ($modi) {
                 $this ->success('广告暂停使用,无法正常显示',url("advdeal/advdeal"));
             } 
        }
        else{
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('astop',0);
            if ($modi) {
                 $this ->error('广告取消暂停，广告正常显示',url("advdeal/advdeal"));
             } 

        }
    }
    public function boxDeal()
    {  
        if (input('check')==null) {
            $adv=db('adv')
            ->where('atype',1)
            ->select();
        }else{
            $adv=db('adv')
            ->where('atype',1)
            ->where('atitle','like','%'.input('check').'%')
            ->select();
        }
        $advshow=db('adv')
        ->where('atype',1)
        ->where('acheck',1)
        ->where('astop',0)
        ->select();
        foreach ($adv as $key => &$value) {
            if ($value['acheck']==0) {
                $value['acheck']='未审核(点击通过)';
                $value['checkcolor']='red';
                if ($value['astop']==0) {
                    $value['stopcolor']='';
                }
                else{
                     $value['stopcolor']='red';
                     $value['checkcolor']='';
                }

            }
            elseif($value['acheck']==1){
                $value['acheck']='通过审核(点击取消)';
                $value['checkcolor']='red';
                 if ($value['astop']==0) {
                    $value['stopcolor']='';
                }
                else{
                     $value['stopcolor']='red';
                     $value['checkcolor']='';
                }

            }
        };
       return view('',['adv'=>$adv,'advshow'=>$advshow]);
    }

    public function boxAdd()
    { 
       return view();
    }

    public function boxDoAdd()
    {  
       $file = request()->file("aimg");
       // 存储到指定路径
       $path = ROOT_PATH."public/static/img/adv/";
       $info = $file ->validate(['size'=>102400,'ext'=>'jpg,png,gif']) ->move($path);
       // 
       $advtitle=input('atext');
       $advcon=input('acontent');
       $apath=input('apath');
       if($info){
            $filename = $info ->getSaveName();
             $adv= [
            'atitle'=>$advtitle,
            'acontent'=>$advcon,
            'aimg'=>$filename,
            'apath'=>$apath,
            'atime'=>time(),
            'acheck'=>0,
            'astop'=>0,
            'atype'=>1
                ];
            $res=db("adv")
                ->insert($adv);
            if ($res) {
                $this->success("轮播图数据添加成功，需要进行审核才能正常使用","advdeal/boxdeal");
                }
            else{
                
                $this->error("轮播图数据添加失败","advdeal/boxdeal");
            }
        }
    }
    public function boxInfo()
    {  
       
       $adv =db('adv')
       ->where('aid',input('aid'))
       ->select();

        return view('',["adv"=>$adv]);

    }
    public function boxInfoModi()
    {  

        $find=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $aaa=$find['aimg'];
       $file = request()->file("aimg");
       // 存储到指定路径
       if ($file) {
       $path = ROOT_PATH."public/static/img/adv/";
       $info = $file ->validate(['size'=>102400,'ext'=>'jpg,png,gif']) ->move($path);
       }
       else{
        $info=0;
       }
       
       // 
       $advtitle=input('atext');
       $advcon=input('acontent');
       $apath=input('apath');
       if($info){
            $filename = $info ->getSaveName();
            $adv = db('adv')
            ->where('aid',input('aid'))
            ->setField(['atitle'=>$advtitle,'acontent'=>$advcon,'aimg'=>$filename,'apath'=>$apath,'atime'=>time(),
                'acheck'=>0]);
            if ($adv) {
                unlink(ROOT_PATH."public\static\img\adv\\".$aaa);
                $this->success("轮播图数据修改成功，需要进行审核才能正常使用","advdeal/boxdeal");
                }
            else{
                
                $this->error("轮播图数据修改失败","advdeal/advdeal");
            }

        }else{
            $adv = db('adv')
                ->where('aid',input('aid'))
                ->setField(['atitle'=>$advtitle,'acontent'=>$advcon,'apath'=>$apath,'atime'=>time(),
                    'acheck'=>0]);
                if ($adv) {
                    $this->success("轮播图数据修改成功，需要进行审核才能正常使用","advdeal/boxdeal");
                    }
                else{
                    
                    $this->error("轮播图数据修改失败","advdeal/advdeal");
                }
        }
    }
    public function boxDel()
    {  

        $find=db('adv')
        ->where('aid',input('aid'))
        ->find();


        $aaa=$find['aimg'];


        $advdel=db('adv')
        ->where('aid',input('aid'))
        ->delete();
         if ($advdel) {
                unlink(ROOT_PATH."public\static\img\adv\\".$aaa);
                $this ->success('删除轮播图成功',url("advdeal/boxdeal"));
            }
            else{
                $this ->error('删除轮播图失败',url("advdeal/boxdeal"));
            }

    }

    public function boxOK()
    {  
        $advcheck=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $check=$advcheck['acheck'];
        if ($check==0) {
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('acheck',1);
            if ($modi) {
                 $this ->success('轮播图审核通过',url("advdeal/boxdeal"));
             } 
        }
        else{
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('acheck',0);
            if ($modi) {
                 $this ->error('轮播图审核取消，无法正常显示',url("advdeal/boxdeal"));
             } 

        }
    

    }
    public function boxStop()
    {  
        $advcheck=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $check=$advcheck['astop'];
        if ($check==0) {
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('astop',1);
            if ($modi) {
                 $this ->success('轮播图暂停使用,无法正常显示',url("advdeal/boxdeal"));
             } 
        }
        else{
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('astop',0);
            if ($modi) {
                 $this ->error('轮播图取消暂停，广告正常显示',url("advdeal/boxdeal"));
             } 

        }
    }
}